import 'package:flutter/material.dart';
import 'package:flutter_app/state/user.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';

import './page/home.dart';
import './page/launch.dart';
import './page/login.dart';

final List<String> whiteList = ["/"];

final GoRouter routerConfig = GoRouter(
  routes: [
    GoRoute(
      path: '/launch',
      name: "launch",
      builder: (context, state) => const Launch(),
    ),
    GoRoute(
      path: '/',
      name: "home",
      builder: (context, state) => const Home(),
    ),
    GoRoute(
      path: '/login',
      name: "login",
      builder: (context, state) => const Login(),
    ),
  ],
  redirect: (BuildContext context, GoRouterState state) {
    /// 未登录用户跳转launch
    if (!Provider.of<UserStore>(context).isLogin) {
      return "/launch";
    } else if (!whiteList.any((w) => w == state.fullPath)) {
      return "/";
    } else {
      return null;
    }
  },
);
